import { userStore } from '@/store/user.js';
import { ElNotification } from 'element-plus'
import 'element-plus/es/components/notification/style/css';
import { h } from 'vue';

import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import router from '@/router/index.js';

declare module 'axios' {
    interface InternalAxiosRequestConfig {
        showMsg?: boolean;
    }
}

// 定义基础响应类型
interface BaseResponse<T = any> {
    msg: any;
    code: number;
    message: string;
    data: T;
}

interface PostOptions {
    showMsg?: boolean; // 是否显示错误信息
    data?: any;         // 可选：请求体数据
    params?: any;       // 可选：URL查询参数
    headers?: Record<string, string>; // 可选：请求头
    // 可以根据需要添加其他配置项
}
// 创建封装类
class HttpClient {
    private instance: AxiosInstance;

    constructor(config: AxiosRequestConfig) {
        this.instance = axios.create(config);

        // 请求拦截器
        this.instance.interceptors.request.use(
            (config) => {
                // 可在此添加token等全局headers
                config.headers = config.headers || {};
                config.headers['X-Requested-Token'] = userStore().userInfo.token;
                config.headers.showMsg = config.showMsg || false;
                return config;
            },
            (error) => {
                return Promise.reject(error);
            }
        );

        // 响应拦截器
        this.instance.interceptors.response.use(
            (response: AxiosResponse<BaseResponse>) => {
                // 统一处理业务逻辑错误
                if (response.data.code !== 200) {
                    if (response.config.headers.showMsg == 'true') {
                        ElNotification({
                            title: '',
                            duration: 3000,
                            message: h('div', { class: '' }, [
                                h('div', { class: 'w-full  flex items-center' }, [
                                    h('div', { class: 'iconfont icon-error text-red-500 text-[20px]' }),
                                    h('div', { class: 'text-black text-[14px] ml-[8px]' }, `${response.data.msg}`)
                                ]),
                            ]),
                        })
                    }
                    if (response.data.code === 201 || response.data.code === 202) {
                        userStore().loginOut()
                        // router.replace('/login')
                    }
                    return Promise.reject(response.data);
                }
                return response.data.data; // 直接返回data字段
            },
            (error) => {
                // 统一处理HTTP错误
                return Promise.reject(error);
            }
        );
    }

    // 封装通用请求方法
    public async request<T>(config: AxiosRequestConfig): Promise<T> {
        return this.instance.request<BaseResponse<T>, T>(config);
    }

    // 封装快捷方法
    public get<T>(url: string, PostOptions?: PostOptions): Promise<T> {
        return this.request({ method: 'GET', url, ...PostOptions });
    }

    public post<T>(url: string, PostOptions: PostOptions): Promise<T> {
        return this.request({ method: 'POST', url, ...PostOptions });
    }

    // 可继续封装put/delete等方法...
}

export default HttpClient